import time
import requests

def group_9(ehrid, headers_8,DOMAIN):

    dict_group_9 = {"疾病史":None, "手术史":None, "外伤史":None, "输血史":None}
    try:
        url_group_9 = f"{DOMAIN}/phis/app/medHistory?search_EQ_ehrId={ehrid}&search_EQ_historyType=1"
        data = {
            "_dch": f"{time.time()}".replace(".", "")[:13],
        }

        response_9 = requests.post(url_group_9, data=data, headers=headers_8)

        list_jibingshi = ["无", "高血压", "糖尿病", "冠心病", "慢性阻塞性肺疾病", "恶性肿瘤", "脑卒中", "重性精神病", "结核病", "肝炎", "其他法定传染病", "职业病", "其他",
                          "高脂血证", "骨关节病", "哮喘", "慢性肾功能衰竭", "脂代谢紊乱", "心房颤动", "短暂性脑缺血发作(TIA)", "心力衰竭", "心脏瓣膜病", "周围血管病",
                          "肝硬化", "慢性肾功能不全", "风湿性关节炎", "痴呆", "良性肿瘤","妇女病检查"]
        li_jibing = response_9.json()["content"]
        list_jibing_big = []
        for li in li_jibing:
            list_jibing_little = {}
            list_jibing_little["疾病"] = list_jibingshi[int(li["optionType"]) - 1]
            try:
                list_jibing_little["确诊日期"] = li["occurrenceTime"]
            except:
                list_jibing_little["确诊日期"] = " "
            try:
                list_jibing_little["描述"] = li["description"]
            except:
                list_jibing_little["描述"] = " "
            list_jibing_big.append(list_jibing_little)
        if len(list_jibing_big) == 0:
            list_jibing_big = "无"
        dict_group_9["疾病史"] = list_jibing_big
        if dict_group_9["疾病史"] == []:
            dict_group_9["疾病史"] = "无"
    except:
        dict_group_9["疾病史"] = "疾病史请求上错误"


    try:
        url_group_9_2 = f"{DOMAIN}/phis/app/medHistory?search_EQ_ehrId={ehrid}&search_EQ_historyType=2"

        response_9_2 = requests.post(url_group_9_2, data=data, headers=headers_8)
        shoushu_info = response_9_2.json()["content"]
        list_shoushu_big = []
        for li in shoushu_info:
            list_shoushu_little = {}
            list_shoushu_little["手术名称" ] = li["description"]
            list_shoushu_little["手术时间" ] = li["occurrenceTime"]
            list_shoushu_big.append(list_shoushu_little)

        dict_group_9["手术史"] = list_shoushu_big
        if len(list_shoushu_big)==0:
            dict_group_9["手术史"] = "无"
        if dict_group_9 == []:
            dict_group_9["手术史"] = "无"
    except:
        dict_group_9["手术史"] = "手术史请求上错误"


    try:
        url_group_9_3 = f"{DOMAIN}/phis/app/medHistory?search_EQ_ehrId={ehrid}&search_EQ_historyType=3"
        response_9_3 = requests.post(url_group_9_3, data=data, headers=headers_8)
        waishang_info = response_9_3.json()["content"]
        list_waishang_big = []
        for li in waishang_info:
            list_waishang_little = {}
            list_waishang_little["外伤"]=li["description"]
            list_waishang_little["外伤时间"] = li["occurrenceTime"]
            list_waishang_big.append(list_waishang_little)
        dict_group_9["外伤史"] = list_waishang_big
        if dict_group_9["外伤史"] ==  []:
            dict_group_9["外伤史"] = "无"
    except:
        dict_group_9["外伤史"] ="外伤史请求上错误"


    try:
        url_group_9_4 = f"{DOMAIN}/phis/app/medHistory?search_EQ_ehrId={ehrid}&search_EQ_historyType=4"
        response_9_4 = requests.post(url_group_9_4, data=data, headers=headers_8)
        shuxue_info = response_9_4.json()["content"]
        list_shuxue_big = []
        for li in shuxue_info:
            list_shuxue_little = {}
            list_shuxue_little["输血原因"]=li["optionType"]
            list_shuxue_little["输血时间"]=li["occurrenceTime"]
        dict_group_9["输血史"] = list_shuxue_big
        if dict_group_9["输血史"] == []:
            dict_group_9["输血史"] = "无"

    except:
        dict_group_9["输血史"] = "输血史请求上错误"

    print('问题没有9',dict_group_9)
    return dict_group_9

if __name__ == '__main__':
    COOKIE = "58FF46F2224EB9D49E4A9E1AA47877FC"
    DOMAIN = "http://10.216.11.24:8306"
    ehrId = "d44928e0-fab7-4838-a15e-466a17d59650"

    headers_8 = {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "cookie": f"JSESSIONID={COOKIE}",
        "host": DOMAIN.strip("http://")
    }
    print(group_9(ehrId,headers_8,DOMAIN))
